/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author MinhVu
 */
public class WebProxy {
    public static void main(String[] argc) throws IOException 
    {

        ServerSocket welcomeSocket = new ServerSocket(1234); //Tao port sever
        System.out.println("Server Start");
        Socket s;
        Proxy x = null;
        while (true)
        {
                //System.out.println("Active Threads: "+ProxyThread.activeThreads());
                s = welcomeSocket.accept();
                //Tao mot thread moi voi socket duoc ket noi toi 1 client da duoc accept
                x = new Proxy(s);
                s.close();
        }
  
    }
}
class Proxy extends Thread
{
    private Socket client_sock;
    Socket proxySocket ; 
    private String fromClient ;
    InputStreamReader client_in ;

    public Proxy(Socket s) throws IOException
    {
         client_sock = s;
         
         client_in = new InputStreamReader(client_sock.getInputStream());
          BufferedReader inFromClient = new BufferedReader(client_in);
        //Lay dia chi host de tao ket noi ra server
        int k =0;
        String host=null;
        String request_Client = null;

        while((fromClient = inFromClient.readLine()) !=null)
        {
            request_Client += fromClient+"\n";
            int x = fromClient.indexOf("Host");
            if(x==0)
            {
                host = fromClient.substring(6);
                break;
            }
                k++;
        }
        //System.out.println(request_Client);
        PrintWriter  out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proxySocket.getOutputStream())));
        out.println(request_Client); 
        out.println(); 
        out.flush();
        proxySocket = new Socket(host,80); 
        InputStream fromServer = proxySocket.getInputStream();
        OutputStream toClient = client_sock.getOutputStream();
        copyStream(fromServer,toClient);
        //
        //        BufferedReader r = new BufferedReader(new InputStreamReader(fromServer));
        //        String a ;
        //        System.out.println("END");
        //            System.out.println(a);
        //        System.out.println("END");
        
        PrintWriter out_to_client = new PrintWriter(new BufferedWriter(new OutputStreamWriter(toClient)));
        out_to_client.println(); 
        out.flush();
    }
        @Override
    public void run()
    {
    }
        public static void copyStream(InputStream input, OutputStream output)
    throws IOException
    {
        byte[] buffer = new byte[1024]; // Adjust if you want 
        int bytesRead;
        while((bytesRead  = input.read(buffer))!= -1)
        {
            output.write(buffer, 0, bytesRead);
            if(bytesRead<1024)
                return;
        }
    }
}  
